package com.tools.cleanmaster.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.hm.ads.sdk.HmAdsSDK;
import com.hm.adsbuisiness.bean.daswipe.HmSwipeAdPlace;
import com.hm.adsbuisiness.service.AdActionException;
import com.swipe.i.w;
import com.tools.ad.util.StatusBarUtil;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.ui.base.LOverlayRationale;
import com.tools.cleanmaster.ui.base.SingleActivity;
import com.tools.cleanmaster.ui.view.header.HeadHelper;


/**
 * desc: 快滑设置页面
 * date: 2018/6/29 16:45
 * author: ancun
 */
public class SwipeSettingActivity extends SingleActivity {

    private ImageView mSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_swipe_setting);
        StatusBarUtil.setTransparent(this);

        HeadHelper.setupMainHeader(this, R.id.titlebar, R.string.du_swipe_settting_title, this).hideSettingButton().setBackgroundColor();

        mSwitch = findViewById(R.id.setting_switch);
        mSwitch.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                requestOverlay(new LOverlayRationale());
            }
        });

    }

    @Override
    protected void doOnResume() {
        handleSwitchStatus();
    }

    private void handleSwitchStatus() {
        boolean swipeEnable = imContext.getADModule().getADSharedPrefManager().getSwipeAdEnable();
        mSwitch.setImageDrawable(swipeEnable ? getResources().getDrawable(R.drawable.item_switch_on) : getResources().getDrawable(R.drawable.item_switch_off));
    }

    @Override
    protected void onOverlayPermissionGranted() {
        super.onOverlayPermissionGranted();
        optionSwipeSDK();
    }

    @Override
    protected void onOverlayPermissionDenied() {
        super.onOverlayPermissionDenied();
    }

    private void optionSwipeSDK() {
        boolean swipeEnable = imContext.getADModule().getADSharedPrefManager().getSwipeAdEnable();
        imContext.getADModule().getADSharedPrefManager().setSwipeAdEnable(!swipeEnable);

        setSwipe(!swipeEnable);

        handleSwitchStatus();
    }

    private void setSwipe(boolean enable) {
        HmSwipeAdPlace swipeAdPlace = HmAdsSDK.getInstance().getDuSwipeAdPlace();
        if (swipeAdPlace != null) {
            try {
                w.a().c(false);
                swipeAdPlace.setSwipeOpen(enable);
            } catch (AdActionException e) {
                e.printStackTrace();
            }
        }
    }

}